//
//  YCOrderTimeListViewController.m
//  YCVillageDoctor
//
//  Created by Mac on 2017/6/14.
//  Copyright © 2017年 zhangxiaoliang. All rights reserved.
//

#import "YCOrderTimeListViewController.h"
#import "YCOrderTimeCell.h"
#import "YCOrderTimeModel.h"

NSString *const cellId = @"YCOrderTimeCell";

@interface YCOrderTimeListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)NSMutableArray <YCOrderTimeModel *>*modelList;

@property(nonatomic,weak)UITableView *tableView;


@property (nonatomic, assign) NSInteger pageNum;

@end

@implementation YCOrderTimeListViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _pageNum = 1;
    
    _modelList = [NSMutableArray array];
    
    self.title = @"包月时间";
    
    UITableView *tableView = [[UITableView alloc]init];
    
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    tableView.delegate = self;
    
    tableView.dataSource = self;
    
    self.tableView  = tableView;
    
    tableView.showsVerticalScrollIndicator = NO;
    
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"YCOrderTimeCell" bundle:nil] forCellReuseIdentifier:cellId];
    
    [self loadOrderTimeData];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        self.pageNum = 1;
        [_modelList removeAllObjects];
        [self loadOrderTimeData];
        
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        self.pageNum ++;
        [self loadOrderTimeData];
    }];
   
}

-(void)loadOrderTimeData {
    
    __weak typeof(self)weakSelf = self;
    
    [self presentLoadingTips:nil];
    
    NSString *phone = [YCUserTool sharedTools].linkMobile;
    
    NSDictionary  *params = @{
                              @"page_num":@(self.pageNum).stringValue,
                              @"link_mobile":judgeString(phone),
                              };
    
    [DownloadData post:kGetOrderTimeInfo params:params success:^(id json) {
        
        [self dismissTips];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        if (json&&[json count]>0) {
            
            [_modelList addObjectsFromArray:[YCOrderTimeModel mj_objectArrayWithKeyValuesArray:json]];

            [weakSelf.tableView reloadData];
            
        }else{
            if (_modelList.count==0) {
                
                [weakSelf noDataViewShowWithSuperView:self.view andTitle:@"暂无数据"];
            }
        }
        
    } failure:^(NSError *error) {
        
        [self dismissTips];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [weakSelf.view errorTip:^{
            [weakSelf.view removeTipView];
            [weakSelf loadOrderTimeData];
        }];
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _modelList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YCOrderTimeCell *cell  = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    cell.model = _modelList[indexPath.row];
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 95;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
